package com.pan.test.controller;

import com.pan.test.entity.constants.Constants;
import com.pan.test.entity.dto.SessionShareDto;
import com.pan.test.entity.dto.SessionWebUserDto;
import com.pan.test.entity.enums.ResponseCodeEnum;
import com.pan.test.entity.vo.PaginationResultVO;
import com.pan.test.entity.vo.ResponseVO;
import com.pan.test.utils.CopyTools;
import com.pan.test.utils.StringTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;


public class ABaseController {

    // 定义日志对象
    private static final Logger logger = LoggerFactory.getLogger(ABaseController.class);

    // 定义成功状态码
    protected static final String STATUC_SUCCESS = "success";

    // 定义失败状态码
    protected static final String STATUC_ERROR = "error";

    // 返回成功响应对象
    protected <T> ResponseVO<T> getSuccessResponseVO(T t) {
        ResponseVO<T> responseVO = new ResponseVO<>(ResponseCodeEnum.CODE_200.getCode(), ResponseCodeEnum.CODE_200.getMsg(), t);
        responseVO.setStatus(STATUC_SUCCESS);
        return responseVO;
    }

    // 将分页结果转换为分页响应对象
    protected <S, T> PaginationResultVO<T> convert2PaginationVO(PaginationResultVO<S> result, Class<T> classz) {
        PaginationResultVO<T> resultVO = new PaginationResultVO<>();
        resultVO.setList(CopyTools.copyList(result.getList(), classz));
        resultVO.setPageNo(result.getPageNo());
        resultVO.setPageSize(result.getPageSize());
        resultVO.setPageTotal(result.getPageTotal());
        resultVO.setTotalCount(result.getTotalCount());
        return resultVO;
    }

    // 从session中获取用户信息
    protected SessionWebUserDto getUserInfoFromSession(HttpSession session) {
        return (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
    }


    // 从session中获取分享信息
    protected SessionShareDto getSessionShareFromSession(HttpSession session, String shareId) {
        return (SessionShareDto) session.getAttribute(Constants.SESSION_SHARE_KEY + shareId);
    }


    // 读取文件并返回给客户端
    protected void readFile(HttpServletResponse response, String filePath) {
        // 判断文件路径是否合法
        if (!StringTools.pathIsOk(filePath)) {
            return;
        }
        OutputStream out = null;
        FileInputStream in = null;
        try {
            File file = new File(filePath);
            // 判断文件是否存在
            if (!file.exists()) {
                return;
            }
            in = new FileInputStream(file);
            byte[] byteData = new byte[1024];
            out = response.getOutputStream();
            int len = 0;
            // 读取文件并返回给客户端
            while ((len = in.read(byteData)) != -1) {
                out.write(byteData, 0, len);
            }
            out.flush();
        } catch (Exception e) {
            logger.error("读取文件异常", e);
        } finally {
            // 关闭输出流
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    logger.error("IO异常", e);
                }
            }
            // 关闭输入流
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    logger.error("IO异常", e);
                }
            }
        }
    }
}